﻿using System;
using System.Windows.Forms;
using System.IO;
using FigKey_TestExec_SL.Common;
using FigKey_TestExec_SL.Entity;
using FigKey_TestExec_SL.Controls;
using WinLibrary.JsonManage;
using WinLibrary.Security;
using WinLibrary.DataViewManager;

namespace Test_Operator_Platform.UI
{
    /// <summary>
    /// 项目的导入：默认显示最新项目路径下的测试计划
    /// 提供自选项目路径
    /// </summary>
    public partial class FrmLoadTestplan : Form
    {
        FKBasicEntity basicEntity;
        string selectDir;
        string configFile;
        public FrmLoadTestplan(ref FKBasicEntity entity, string cfgfile)
        {
            InitializeComponent();
            this.basicEntity = entity;
            this.configFile = cfgfile;
        }

        private void FrmLoadTestplan_Load(object sender, EventArgs e)
        {
            if (this.basicEntity != null)
            {
                var testDir = "";
                if (!string.IsNullOrEmpty(this.basicEntity.TestActualProfile))
                {
                    testDir = Path.GetDirectoryName(this.basicEntity.TestActualProfile);
                }
                UpdateTestplan(testDir);
            }

            this.btn_select.Click += Btn_select_Click;
            this.listView1.DoubleClick += ListView1_DoubleClick;
        }

        private void ListView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listView1.Items.Count == 0)
                return;
            if (this.listView1.FocusedItem == null)
                return;
            var testplan = Path.GetFileNameWithoutExtension(this.listView1.FocusedItem.Text);
            this.basicEntity.TestPlanName = testplan;
            //保存新计划路径
            if (Directory.Exists(this.selectDir))
            {
                this.basicEntity.TestActualProfile = this.selectDir + testplan + ".tp";
                ConfigManage.SaveConfig(this.basicEntity, this.configFile);
            }
            else
            {
                var planfile = Path.GetDirectoryName(this.basicEntity.TestActualProfile);
                this.basicEntity.TestActualProfile = planfile + "\\" + testplan + ".tp";
            }

            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void Btn_select_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (Directory.Exists(this.tb_project.Text))
                dialog.SelectedPath = this.tb_project.Text;
            if (dialog.ShowDialog() != DialogResult.OK)
                return;
            if (string.IsNullOrEmpty(dialog.SelectedPath))
                return;
            this.selectDir = dialog.SelectedPath + "\\";
            UpdateTestplan(dialog.SelectedPath);
        }

        private void UpdateTestplan(string rootPath)
        {
            if (string.IsNullOrEmpty(rootPath))
                return;
            //加载默认项目路径   
            this.tb_project.Text = rootPath;
            DataViewUtils.InitListView(this.listView1, 1, new int[] { this.listView1.Width});

            if (!Directory.Exists(rootPath))
            {
                MessageBox.Show("读取测试计划路径不存在！", "警告");
                return;
            }
            var directInfo = new DirectoryInfo(rootPath);

            var files = directInfo.GetFiles();
            if (files.Length > 0)
            {
                this.listView1.Items.Clear();
                foreach (var file in files)
                {
                    if (file.Extension != ".tp")
                        continue;
                    ListViewItem lv = new ListViewItem();
                    lv.Text = file.Name;
                    this.listView1.Items.Add(lv);
                }
            }
        }
    }
}
